package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingInvoiceFeeSettingDocDetailEntity;
import com.ly.heart.domain.KingInvoiceFeeSettingEntity;
import com.ly.heart.domain.KingInvoiceFeeSettingInvoiceDetailEntity;
import com.ly.heart.domain.dto.KingInvoiceFeeSettingDto;
import com.ly.heart.domain.dto.StartProcessDto;
import com.ly.heart.mapper.KingInvoiceFeeSettingMapper;
import com.ly.heart.service.IActModelService;
import com.ly.heart.service.IKingInvoiceFeeSettingDocDetailService;
import com.ly.heart.service.IKingInvoiceFeeSettingInvoiceDetailService;
import com.ly.heart.service.IKingInvoiceFeeSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 发票与费用项目对应设置 服务实现类
 * </p>
 * @author WuZH
 * @since 2024-08-17
 */
@Service
public class KingInvoiceFeeSettingServiceImpl extends ServiceImpl<KingInvoiceFeeSettingMapper, KingInvoiceFeeSettingEntity> implements IKingInvoiceFeeSettingService {

    @Resource
    private IActModelService iActModelService;
    @Resource
    private IKingInvoiceFeeSettingDocDetailService iKingInvoiceFeeSettingDocDetailService;
    @Resource
    private IKingInvoiceFeeSettingInvoiceDetailService iKingInvoiceFeeSettingInvoiceDetailService;


    @Override
    public List<KingInvoiceFeeSettingEntity> queryList (KingInvoiceFeeSettingEntity kingInvoiceFeeSettingEntity) {
        return null;
    }

    @Override
    @Transactional (rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public AjaxResult draft (KingInvoiceFeeSettingDto kingInvoiceFeeSettingDto) {
        KingInvoiceFeeSettingEntity kingInvoiceFeeSettingEntity = kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingEntity ();
        kingInvoiceFeeSettingEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        int count = this.count ();
        kingInvoiceFeeSettingEntity.setDocNo ( "FPYFY".concat ( String.format ( "%08d", count ) ) );
        kingInvoiceFeeSettingEntity.setCreateOrgId ( CurrentUserUtil.getCompanyId () );
        kingInvoiceFeeSettingEntity.setUseOrgId ( CurrentUserUtil.getCompanyId () );
        kingInvoiceFeeSettingEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingInvoiceFeeSettingEntity.setCreateTime ( DateUtils.getNowDate () );
        kingInvoiceFeeSettingEntity.setState ( ConstUtil.STATE_IN_DRAFT );
        boolean save = this.save ( kingInvoiceFeeSettingEntity );
        if ( ! save ) {
            throw new CustomException ( "暂存发票与费用项目设置失败！" );
        }
        updateInvoiceFeeSettingDocDetail ( kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingDocDetailEntities (), kingInvoiceFeeSettingEntity.getId () );
        updateInvoiceFeeSettingInvoiceDetail ( kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingInvoiceDetailEntities (), kingInvoiceFeeSettingEntity.getId () );
        return AjaxResult.success ( "暂存发票与费用项目设置成功", kingInvoiceFeeSettingEntity.getId () );
    }

    @Override
    public AjaxResult submit (KingInvoiceFeeSettingEntity kingInvoiceFeeSettingEntity) {
        if ( StringUtils.isBlank ( kingInvoiceFeeSettingEntity.getId () ) ) {
            return AjaxResult.error ( "请先暂存该单据！" );
        }
        StartProcessDto startDto = new StartProcessDto ().
                setBusinessId ( kingInvoiceFeeSettingEntity.getId () ).
                setBusinessType ( kingInvoiceFeeSettingEntity.getBusinessType () ).setModelId ( kingInvoiceFeeSettingEntity.getModelId () ).
                setBusinessName ( "发票与费用项目设置".concat ( kingInvoiceFeeSettingEntity.getId () ) );
        String instanceId = iActModelService.startProcess ( startDto );
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceFeeSettingEntity> ().
                set ( KingInvoiceFeeSettingEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingInvoiceFeeSettingEntity :: getUpdateTime, DateUtils.getNowDate () ).
                set ( KingInvoiceFeeSettingEntity :: getInstanceId, instanceId ).
                set ( KingInvoiceFeeSettingEntity :: getState, ConstUtil.STATE_IN_PROCESS ).
                eq ( KingInvoiceFeeSettingEntity :: getId, kingInvoiceFeeSettingEntity.getId () ) );
        if ( ! update ) {
            throw new CustomException ( "提交发票与费用项目设置失败！" );
        }
        return AjaxResult.success ( "暂存发票与费用项目设置成功" );
    }

    @Override
    public AjaxResult updateSingle (KingInvoiceFeeSettingDto kingInvoiceFeeSettingDto) {
        KingInvoiceFeeSettingEntity kingInvoiceFeeSettingEntity = kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingEntity ();
        kingInvoiceFeeSettingEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingInvoiceFeeSettingEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingInvoiceFeeSettingEntity );
        if ( ! b ) {
            throw new CustomException ( "更新发票与费用项目设置失败！" );
        }
        updateInvoiceFeeSettingDocDetail ( kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingDocDetailEntities (), kingInvoiceFeeSettingEntity.getId () );
        updateInvoiceFeeSettingInvoiceDetail ( kingInvoiceFeeSettingDto.getKingInvoiceFeeSettingInvoiceDetailEntities (), kingInvoiceFeeSettingEntity.getId () );
        return AjaxResult.success ( "更新发票与费用项目设置成功" );
    }

    @Override
    public AjaxResult deleteSingle (String[] ids) {
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceFeeSettingEntity> ().
                set ( KingInvoiceFeeSettingEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingInvoiceFeeSettingEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingInvoiceFeeSettingEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingInvoiceFeeSettingEntity :: getId, ids ) );
        if ( ! update ) {
            throw new CustomException ( "删除发票与费用项目设置失败！" );
        }
        return AjaxResult.success ( "删除发票与费用项目设置成功" );
    }

    @Override
    public AjaxResult disabled (String id) {
        KingInvoiceFeeSettingEntity feeSettingEntity = this.getById ( id );
        boolean update = this.update ( new LambdaUpdateWrapper<KingInvoiceFeeSettingEntity> ().
                set ( KingInvoiceFeeSettingEntity :: getIsDisabled, feeSettingEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingInvoiceFeeSettingEntity :: getDisabledBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingInvoiceFeeSettingEntity :: getDisabledTime, DateUtils.getNowDate () ).
                eq ( KingInvoiceFeeSettingEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( feeSettingEntity.getIsDisabled () == 1 ? "禁用发票与费用项目设置失败" : "解禁发票与费用项目设置失败" );
        }
        return AjaxResult.success ( feeSettingEntity.getIsDisabled () == 1 ? "禁用发票与费用项目设置成功" : "解禁发票与费用项目设置成功" );
    }

    private void updateInvoiceFeeSettingDocDetail (List<KingInvoiceFeeSettingDocDetailEntity> kingInvoiceFeeSettingDocDetailEntities, String id) {
        if ( ! CollectionUtils.isEmpty ( kingInvoiceFeeSettingDocDetailEntities ) ) {
            iKingInvoiceFeeSettingDocDetailService.remove ( new LambdaUpdateWrapper<KingInvoiceFeeSettingDocDetailEntity> ().eq ( KingInvoiceFeeSettingDocDetailEntity :: getInvoiceFeeSettingId, id ) );
            kingInvoiceFeeSettingDocDetailEntities.forEach ( detail -> {
                detail.setInvoiceFeeSettingId ( id );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = iKingInvoiceFeeSettingDocDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "发票与费用项目对应设置单据其他因素列表" );
                }
            } );
        }
    }

    private void updateInvoiceFeeSettingInvoiceDetail (List<KingInvoiceFeeSettingInvoiceDetailEntity> kingInvoiceFeeSettingInvoiceDetailEntities, String id) {
        if ( ! CollectionUtils.isEmpty ( kingInvoiceFeeSettingInvoiceDetailEntities ) ) {
            iKingInvoiceFeeSettingInvoiceDetailService.remove ( new LambdaUpdateWrapper<KingInvoiceFeeSettingInvoiceDetailEntity> ().eq ( KingInvoiceFeeSettingInvoiceDetailEntity :: getInvoiceFeeSettingId, id ) );
            kingInvoiceFeeSettingInvoiceDetailEntities.forEach ( detail -> {
                detail.setInvoiceFeeSettingId ( id );
                detail.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
                detail.setCreateTime ( DateUtils.getNowDate () );
                boolean save = iKingInvoiceFeeSettingInvoiceDetailService.save ( detail );
                if ( ! save ) {
                    throw new CustomException ( "发票与费用项目对应设置发票其他因数列表失败！" );
                }
            } );
        }
    }
}
